#include "color.h"
#include <cstdlib>
#include <cmath>
#include <iostream>

Color::Color(double _red, double _green, double _blue, double _alpha) : red(_red), green(_green), blue(_blue), alpha(_alpha){
    if(_red < 0) red = 0;
    else if(_red > 1) red = 1;
    else red = _red;

    if(_green < 0) green = 0;
    else if(_green > 1) green = 1;
    else green = _green;

    if(_blue < 0) blue = 0;
    else if(_blue > 1) blue = 1;
    else blue = _blue;

    if(_alpha < 0) alpha = 0;
    else if(_alpha > 1) alpha = 1;
    else alpha = _alpha;
}

Color::Color(void) : red(0) , green(0) , blue(0), alpha(1){
}

Color::Color(const Color& _color) : red(_color.red), green(_color.green), blue(_color.blue), alpha(_color.alpha){
}

Color::~Color(void){
}

void Color::print() const{
    std::cout << "Vermelho: " << red << "\tVerde: " << green << "\tAzul: " << blue << "\tAlfa: " << alpha << std::endl;
}

Color& Color::operator=(const Color& _color) {
	if(this != &_color) {
		red = _color.red;
		green = _color.green;
        blue = _color.blue;
        alpha = _color.alpha;
	}
	return *this;
}

double Color::operator[](int& _index) const {
	switch(_index) {
		case 1: return red;
		case 2: return green;
		case 3: return blue;
		case 4: return alpha;
		default: exit(1);
	}
}

Color Color::operator+(const Color& _color) const {
    double r = _color.red + red;
    double g = _color.green + green;
    double b = _color.blue + blue;

	return Color(r, g, b);
}

Color Color::operator*(const Color& _color) const {
    double r = _color.red * red;
    double g = _color.green * green;
    double b = _color.blue * blue;

	return Color(r, g, b);
}

Color Color::operator-(const Color& _color) const {
    double r = _color.red - red;
    double g = _color.green - green;
    double b = _color.blue - blue;

	return Color(r, g, b);
}

Color Color::operator/(const Color& _color) const {
    double r = _color.red / red;
    double g = _color.green / green;
    double b = _color.blue / blue;

	return Color(r, g, b);
}

Color operator*(const Color& _color, const double _value) {
    double r = _color.red * _value;
    double g = _color.green * _value;
    double b = _color.blue * _value;

	return Color(r, g, b);
}

Color operator*(const double _value, const Color& _color) {
	return _color*_value;
}

double *Color::intoVector() const {
    double *d;
    d = new double[4];
    d[0] = red;
    d[1] = green;
    d[2] = blue;
    d[3] = alpha;

    return d;
}
